package com.rongshu.view.userInfo.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.rongshu.R;
import com.rongshu.utils.UserInfoUtils;
import com.rongshu.view.discovery.bean.AuthorDetailBean;
import com.rongshu.view.home.listener.OnItemMoreClickListener;
import com.widget.CircleImageView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ListManagementAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<AuthorDetailBean> mAuthorDetailBeans = null;
    private Context mContext = null;
    //type
    private int type = -1;
    public static final int BLACK = 0x01;
    public static final int ATTENTION = 0x02;
    public static final int FANS = 0x03;
    private OnItemMoreClickListener listener = null;

    public ListManagementAdapter(Context context, int type) {
        this.mContext = context;
        this.type = type;
    }

    public void setAuthorDetailBeans(List<AuthorDetailBean> list) {
        this.mAuthorDetailBeans = list;
    }

    public void addAuthorDetailBeans(List<AuthorDetailBean> list) {
        this.mAuthorDetailBeans.addAll(list);
    }

    public List<AuthorDetailBean> getAuthorDetailBeans() {
        return this.mAuthorDetailBeans;
    }

    public int getType() {
        return type;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_blacklist_management_item, parent, false);
        return new BlackListViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {
        final BlackListViewHolder holder = (BlackListViewHolder) viewHolder;
        final AuthorDetailBean bean = mAuthorDetailBeans.get(position);
        //头像
        RequestOptions options = new RequestOptions()
                .centerCrop()
                .placeholder(R.drawable.user_head_icon);
        Glide.with(mContext)
                .load(bean.getAvatar())
                .apply(options)
                .into(holder.blacklist_circle_imageview);
        //vip
        holder.iv_vip.setVisibility(bean.getVip() ? View.VISIBLE : View.GONE);
        //bi_name
        holder.blacklist_user_name.setText(bean.getBi_name());
        //状态
        switch (type) {
            case BLACK://黑名单
                holder.txt_remove_blacklist.setTextColor(mContext.getResources().getColor(R.color.color_3cc051));
                holder.txt_remove_blacklist.setText(mContext.getString(R.string.remove_blacklist));
                break;
            case ATTENTION://我的关注
                holder.txt_remove_blacklist.setTextColor(bean.getIs_mutual() == 1 ?
                        mContext.getResources().getColor(R.color.color_9f9f9f) : bean.getIs_guanzhu() == 1 ?
                        mContext.getResources().getColor(R.color.color_9f9f9f) : mContext.getResources().getColor(R.color.color_9f9f9f));
                //is_mutual 是否相互关注(1是 0否),is_guanzhu是否关注(1是 0否)
                holder.txt_remove_blacklist.setText(bean.getIs_mutual() == 1 ?
                        mContext.getString(R.string.mutual_attention) : bean.getIs_guanzhu() == 1 ?
                        mContext.getString(R.string.already_attention) : "");
                break;
            case FANS://我的粉丝
                holder.txt_remove_blacklist.setTextColor(bean.getIs_mutual() == 1 || bean.getIs_guanzhu() == 1 ?
                        mContext.getResources().getColor(R.color.color_9f9f9f) :
                        mContext.getResources().getColor(R.color.color_3cc051));
                //is_mutual 是否相互关注(1是 0否)
                holder.txt_remove_blacklist.setText(bean.getIs_mutual() == 1 ?
                        mContext.getString(R.string.mutual_attention) :
                        bean.getIs_guanzhu() == 1 ?
                                mContext.getString(R.string.already_attention) : mContext.getString(R.string.add_attention));
                holder.iv_add.setVisibility(bean.getIs_mutual() == 1 || bean.getIs_guanzhu() == 1 || bean.getUser_id() == UserInfoUtils.getUserId() ? View.GONE : View.VISIBLE);
                holder.txt_remove_blacklist.setVisibility(bean.getUser_id() == UserInfoUtils.getUserId() ? View.GONE : View.VISIBLE);
                break;
        }
        //item click
        holder.txt_remove_blacklist.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onItemClick(type == ATTENTION ? holder.itemView
                            : type == FANS ? holder.layout_status : null, position);
                }
            }
        });
        //header click
        holder.blacklist_circle_imageview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onUserHeaderClick(position, bean.getUser_id_to(), (bean.getIs_allow_browse_homepage() == 1));
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mAuthorDetailBeans == null ? 0 : mAuthorDetailBeans.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    public static class BlackListViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.blacklist_circle_imageview)
        CircleImageView blacklist_circle_imageview;
        @BindView(R.id.layout_status)
        LinearLayout layout_status;
        @BindView(R.id.blacklist_user_name)
        TextView blacklist_user_name;
        @BindView(R.id.txt_remove_blacklist)
        TextView txt_remove_blacklist;
        @BindView(R.id.iv_vip)
        ImageView iv_vip;
        @BindView(R.id.iv_add)
        ImageView iv_add;

        public BlackListViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    /**
     * set item litener
     *
     * @param listener
     */
    public void setOnItemChildClickListener(OnItemMoreClickListener listener) {
        this.listener = listener;
    }
}
